<?php
/**
 * @author Zwc
 * Created On 2021/3/9 22:11
 */

namespace app\auth\service;


use app\common\models\AdminGroupModel;
use app\common\models\GroupMenuModel;
use app\common\models\GroupModel;

class GroupService extends BaseService
{
    protected $model;
    public function __construct()
    {
        $this->model = new GroupModel();
        parent::__construct();
    }

    /**
     * 获取数据
     * @param $params
     */
    public function getData($params) {
        $condition = $this->getCondition();
        $count = $this->model->where($condition)->count();
        $datas = $this->model->where($condition)->order('id desc')->page($params['page'],$params['limit'])
            ->select();
        return ['count' => $count,'data' => $datas];
    }

    /**
     * 添加菜单
     * @return mixed|void
     */
    public function add($params) {
        // 添加角色
        $data = [
            'title' => $params['title'],
            'remark' => $params['remark']
        ];
        $groupId = $this->model->insertGetId($data);
        if(count($params['menus']) > 0) {
            $datas = [];
            for ($i=0;$i<count($params['menus']);++$i) {
                $datas[] = [
                    'group_id' => $groupId,
                    'menu_id' => $params['menus'][$i]
                ];
            }
            $groupMenuModel = new GroupMenuModel();
            $res = $groupMenuModel->insertAll($datas);
        }
        if($groupId > 0) {
            return ['code' => 0,'msg' => '操作成功'];
        }else {
            return ['code' => 1,'msg' => '操作失败'];
        }
    }

    public function edit($params)
    {
        $groupModel = new GroupModel();
        $data = [
            'title' => $params['title'],
            'remark' => $params['remark']
        ];
        $where = [
            'id' => $params['id']
        ];
        $res = $groupModel->where($where)->save($data);
        if($res !== false) {
            //删掉原来的菜单
            $where = [
                'group_id' => $params['id']
            ];
            $updateData = [
                'del_flag' => 1
            ];
            $groupMenuModel = new GroupMenuModel();
            $groupMenuModel->where($where)->save($updateData);
            // 添加新的菜单
            if(count($params['menus']) > 0) {
                $datas = [];
                for ($i=0;$i<count($params['menus']);++$i) {
                    $datas[] = [
                        'group_id' => $params['id'],
                        'menu_id' => $params['menus'][$i],
                    ];
                }
                $groupMenuModel = new GroupMenuModel();
                $groupMenuModel->insertAll($datas);
            }
        }
        if($res !== false) {
            return ['code' => 0,'msg' => '操作成功'];
        }else {
            return ['code' => 1,'msg' => '操作失败'];
        }
    }

    /**
     * 删除角色
     */
    public function del($id) {
        $where = [
            'id' => $id
        ];
        $updateData = [
            'del_flag' => 1
        ];
        $groupModel = new GroupModel();
        $res = $groupModel->where($where)->save($updateData);
        if($res !== false) {
            return ['code' => 0,'msg' => '操作成功'];
        }else {
            return ['code' => 1,'msg' => '操作失败'];
        }
    }

    private function getCondition() {
        $condition = [
            'del_flag' => 0
        ];
        return $condition;
    }

    /**
     * 查看详情
     * @param $id
     * @return array|void
     */
    public function detail($id) {
        $model = new GroupModel();
        $group = $model->where(['id' => $id])->find();
        // 获取已经选中的菜单
        $condition = [
            'del_flag' => 0,
            'group_id' => $id
        ];
        $menuModel = new GroupMenuModel();
        $groupMenuData = $menuModel->where($condition)->select();
        $menuIdArr = [];
        foreach ($groupMenuData as $val) {
            if($val['action_id'] > 0) {
                $menuIdArr[] = $val['menu_id'].'_'. $val['action_id'];
            }else {
                $menuIdArr[] = $val['menu_id'];
            }
        }
        $group['menus'] = implode(',',$menuIdArr);
        return $group;
    }

    /**
     * 获取所有的角色
     */
    public function getAllGroup() {
        $condition = [
            'del_flag' => 0
        ];
        $model = new GroupModel();
        $data = $model->where($condition)->field('id,title')->select();
        return $data;
    }

    /**
     * 根据用户ID获取用户的角色
     * @param $list
     */
    public function getGroupNameByList($list) {
        // 获取所所有的角色
        $groupModel = new GroupModel();
        $groupList = $groupModel->where(['del_flag' => 0])->select();
        $datas = [];
        foreach ($groupList as $val) {
            $datas[$val['id']] = $val['title'];
        }
        $condition = [
            ['user_id','IN',$list],
            ['del_flag','=',0]
        ];
        $model = new AdminGroupModel();
        $data = $model->where($condition)->select();
        $userGroup = [];
        foreach ($data as $val) {
            $userGroup[$val['user_id']] = $datas[$val['group_id']];
        }
        return $userGroup;
    }
}